
var AudioMgr = require("AudioMgr");
var GameDataMgr = require("GameDataMgr");
var config = require("config");
var dialogManager = require("dialogManager");
function initMgr(){
    cc.vv = {};
    cc.vv.http = require("HTTP");

    cc.vv.gameDataMgr = new GameDataMgr();
    cc.vv.gameDataMgr.init();

    cc.vv.audioMgr = new AudioMgr();
    cc.vv.audioMgr.init();
}

cc.Class({
    extends: cc.Component,
    properties: {
        labLoading : {
            default : null,
            type : cc.Label
        },
        img_bg : {
            default : null,
            type : cc.Node
        },
    },
    // use this for initialization
    onLoad: function () {
        // config.loadImage(this.img_bg,"first_bg","resources/image/");
        console.log("typeof(wx):"+typeof(wx));
        if (cc.sys.OS_ANDROID == cc.sys.os && config.Publish == "android"){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","removeLaunchImage","()V");
        }  

        if(typeof(wx)=="undefined"){return;}
        //设置保持常亮状态
        wx.setKeepScreenOn({
            keepScreenOn:true,
        });
        wx.showShareMenu({
            withShareTicket: true,
        });
        var shareImg = config.getShareImgPath("3");
        wx.onShareAppMessage(function(res){
            return {
                title: config.shareTxt["3"][0],
                imageUrl: shareImg,
                query :  "key="+config.weiXinCode,
                success(res){
                    console.log("转发成功!!!");
                },
                fail(res){
                    console.log("转发失败!!!")
                } 
            }
        });
    },
    start () {
        var self = this;
        initMgr();
        if(typeof(wx)=="undefined"){
            var fn = function(){
                self.downloadFile();
            }
            setTimeout(fn,1000);
        }else{
            // this.getWxUserInfo();
            this.weixinLogin();
        }
    },
    onDestroy(){
        console.log("appStart Destroy");
    },
    preloadNextScene(){
        cc.director.preloadScene("hallScene", function () {
            cc.director.loadScene("hallScene");
        });
    },
    weixinLogin(){
        var self = this;
        wx.login({
            success: function(ress) {
                if (ress.code) {
                    console.log("登录成功:",ress);
                    config.weiXinCode = ress.code;
                    self.getWxUserInfo();
                }else{
                    console.log('登录失败！',ress)
                }
            }
        });
    },
    //获取微信用户信息
    getWxUserInfo(){
        var self = this;
        var launch = wx.getLaunchOptionsSync();
        console.log("launch =  " + JSON.stringify(launch));
        self.shareUid =  launch.query.key;
        console.log("shareUid =  " + self.shareUid);

        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.userInfo']) {
                    wx.authorize({
                        scope: 'scope.userInfo',
                        success() {
                            // 用户已经同意
                            console.log("用户已经同意,用户信息授权");
                            wx.getUserInfo({
                                success: function (res) {
                                    console.log("res.userInfo = " + JSON.stringify(res.userInfo));
                                    config.UpdateWxInfo(res.userInfo);
                                    self.downloadFile();
                                },
                                fail: function (res){
                                    console.log("获取信息失败!!!")
                                    dialogManager.showAuthorizeDialog();
                                } 
                            });
                        },
                        fail: function (res){
                            console.log("用户拒绝同意!!!")
                            dialogManager.showAuthorizeDialog();
                        } 
                    })
                }else{
                    console.log("用户信息授权 成功");
                    wx.getUserInfo({
                        success: function (res) {
                            console.log("res.userInfo = " + JSON.stringify(res.userInfo));
                            config.UpdateWxInfo(res.userInfo);
                            self.downloadFile();
                        },
                        fail: function (res){
                            console.log("获取信息失败!!!")
                            dialogManager.showAuthorizeDialog();
                        } 
                    });
                }
            }
        });
    },
    //下载资源包
    downloadFile(){
        var self = this;
        if(typeof(wx)=="undefined"){
            self.preloadNextScene();
        }else{

            var fileManager = wx.getFileSystemManager();
            var soundspath = wx.env.USER_DATA_PATH+"/yxxqq_res/1.0.1";
            console.log("soundspath:"+soundspath);
            fileManager.access({
                path:soundspath,
                success(res){
                    console.log("目录存在!!!",res);
                    self.preloadNextScene();
                },
                fail(res){
                    console.log("目录不存在!!!",res);
                    var downloadTask = wx.downloadFile({
                        url: 'https://127.0.0.1/gemen/yxxqq_res_101.zip',
                        success: function(res) {
                            console.log("资源下载成功");
                            var filePath = res.tempFilePath;
                            fileManager.unzip({
                                zipFilePath:filePath,
                                targetPath:wx.env.USER_DATA_PATH,
                                success(res){
                                    console.log("解压成功!!!",res)
                                    self.preloadNextScene();
                                },
                                fail(res){
                                    console.log("解压失败!!!",res)
                                } 
                            });
                        },
                        fail(res){
                            console.log("资源下载失败",res);
                        } 
                    });
                    downloadTask.onProgressUpdate((res) => {
                        self.labLoading.string = "资源更新中..."+res.progress+"%";
                        // console.log('下载进度', res.progress);
                        // console.log('已经下载的数据长度', res.totalBytesWritten);
                        // console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite);
                    })
                            
                } 
            });  
        }
    }
});
